package com.haizhi.crm.sys.auth.dao;

import com.haizhi.crm.sys.auth.model.ShareTargetUserVo;
import com.haizhi.crm.sys.auth.model.UserDo;
import com.haizhi.crm.sys.auth.model.UserResultVo;
import com.haizhi.crm.sys.auth.model.UserVo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by chengmo on 2018/5/15.
 */
@Transactional
@Repository
public interface UserDao extends JpaRepository<UserDo, Long> {

    @Query(value = "select new com.haizhi.crm.sys.auth.model.UserVo" +
            "(u.userNo,u.id,u.name,u.sex,i.id,i.name,d.name,p.id,p.name,u.email,u.phone) " +
            "from UserDo u,PositionDo p,DepartmentDo d,InstitutionDo i " +
            "where u.positionId=p.id and u.departmentId=d.id and d.institutionId=i.id " +
            "and u.id=:id" )
    UserVo findBy(@Param("id" ) Long id);

    UserDo findFirstByUserNoEquals(String userNo);

    @Query(value = "select new com.haizhi.crm.sys.auth.model.UserVo(id,userNo) " +
            "from UserDo where userNo in :userNos" )
    List<UserVo> findByUserNos(@Param("userNos" ) String[] userNos);


    /**
     * 根据用户id集合，查询用户集合
     *
     * @param visitorIds
     * @return
     */
    @Query("select new com.haizhi.crm.sys.auth.model.UserResultVo (u.id,u.userNo,u.name) " +
            "from UserDo u where u.id in :visitorIds order by u.id desc" )
    List<UserResultVo> queryUsersByIds(@Param("visitorIds" ) Long[] visitorIds);

    @Query("select new com.haizhi.crm.sys.auth.model.UserResultVo (u.id,'',u.name) " +
            "from UserDo u where u.departmentId =:deptId and u.id <>:userId" )
    List<UserResultVo> findByUserIdAndDeptId(@Param("userId" ) Long userId, @Param("deptId" ) Long deptId);

    @Query(value = "SELECT  new  com.haizhi.crm.sys.auth.model.UserResultVo(u.id,u.userNo,u.name) " +
            " from UserDo u where u.name LIKE CONCAT('%',:name,'%')  " )
    List<UserResultVo> findByName(@Param("name" ) String name);

    /**
     * 根据部门id用户ID集合
     *
     * @param departmentId
     * @return
     */
    @Query(value = "select t.id from ts_user t where t.department_id =:departmentId ", nativeQuery = true)
    List<Integer> findByDeptId(@Param("departmentId" ) Long departmentId);

    @Query(value = "select new com.haizhi.crm.sys.auth.model.ShareTargetUserVo" +
            "(id,name) from UserDo  where departmentId =:departmentId " )
    List<ShareTargetUserVo> findByDepartmentId(@Param("departmentId" ) Long departmentId);

    @Query(value = "select new com.haizhi.crm.sys.auth.model.ShareTargetUserVo" +
            "(id,name) from UserDo  where departmentId =:departmentId and name LIKE CONCAT('%',:name,'%')" )
    List<ShareTargetUserVo> findBy(@Param("departmentId" ) Long departmentId, @Param("name" ) String name);

}
